package cn.iocoder.yudao.framework.redis.core;

import cn.iocoder.yudao.framework.redis.core.model.BaseRedisEvent;
import com.alibaba.fastjson.JSON;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.ObjectUtils;

/**
 * redis 订阅与发布的客户端，负责向redis发布消息
 *
 * @author: wds_prc
 * @since: 2024/8/2 16:49
 */
public class RedisPublishClient {

    public final static String COMMON_REDIS_EVENT_CHANNEL = "common_redis_event_channel";
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 向redis 频道发送事件内容
     *
     * @param event
     */
    public void publishMsg(BaseRedisEvent event) {
        if (ObjectUtils.isEmpty(event.getEventId())) {
            throw new IllegalArgumentException("redis 事件的id不能为空,class = " + event.getClass().getName());
        }

        String value = JSON.toJSONString(event);
        stringRedisTemplate.convertAndSend(COMMON_REDIS_EVENT_CHANNEL, value);
    }
}
